package edu.spec.chenli.Listener;

import javax.servlet.ServletContext;
import javax.servlet.ServletContextAttributeEvent;
import javax.servlet.ServletContextAttributeListener;
import javax.servlet.ServletContextEvent;
import javax.servlet.http.HttpSessionEvent;

//@WebListener()
public class MyServletContextAttributeListener implements ServletContextAttributeListener{

    // Public constructor is required by servlet spec
    // -------------------------------------------------------
    // ServletContextListener implementation
    // -------------------------------------------------------
    public void contextInitialized(ServletContextEvent sce) {
      /* This method is called when the servlet context is
         initialized(when the Web application is deployed).
         You can initialize servlet context related data here.
      */
    }

    public void contextDestroyed(ServletContextEvent sce) {
      /* This method is invoked when the Servlet Context
         (the Web application) is undeployed or
         Application Server shuts down.
      */
    }

    // -------------------------------------------------------
    // HttpSessionListener implementation
    // -------------------------------------------------------
    public void sessionCreated(HttpSessionEvent se) {
        /* Session is created. */
    }

    public void sessionDestroyed(HttpSessionEvent se) {
        /* Session is destroyed. */
    }

    // -------------------------------------------------------
    // HttpSessionAttributeListener implementation
    // -------------------------------------------------------

    public void attributeAdded(ServletContextAttributeEvent sbe) {
      /* This method is called when an attribute
         is added to a session.
      */
        ServletContext application=sbe.getServletContext();
        //获取添加的属性值和属性名
        String name=sbe.getName();
        Object value=sbe.getValue();
        System.out.println(application + "范围内添加了名为" +name+ ",值为"+value+"的属性");

    }

    public void attributeRemoved(ServletContextAttributeEvent sbe) {
      /* This method is called when an attribute
         is removed from a session.
      */
        ServletContext application=sbe.getServletContext();
        //获取被删除的属性值和属性名
        String name=sbe.getName();
        Object value=sbe.getValue();
        System.out.println(application + "范围内添加了名为" +name+ ",值为"+value+"的属性被删除了");
    }

    public void attributeReplaced(ServletContextAttributeEvent sbe) {
      /* This method is invoked when an attibute
         is replaced in a session.
      */
        ServletContext application=sbe.getServletContext();
        //获取替换的属性值和属性名
        String name=sbe.getName();
        Object value=sbe.getValue();
        System.out.println(application + "范围内添加了名为" +name+ ",值为"+value+"的属性被替换了");
    }
}
